package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import com.sun.activation.registries.MailcapFile;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.security.pkcs11.wrapper.CK_SESSION_INFO;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class UserController {

//    @RequestMapping("add")
//    public UserInfo add(UserInfo userInfo) {
//        return userInfo;
//    }
    private static String _SESSION_KEY ="SESSION_KEY";

    @RequestMapping("/add")
    public String add(@RequestParam(value = "id",required = false)int uid, String name) {
        return name +"  " + uid;
    }

    @RequestMapping("/json")
    public UserInfo getJson(@RequestBody UserInfo userInfo) {
        return userInfo;
    }

    @RequestMapping("/html")
    public String gethtml() {
        return "<h1>标题1<h1>";
    }

//    @RequestMapping("/detail/{aid}")
//    public Integer detail(@PathVariable("aid") Integer aid) {
//        return aid;
//    }

    @RequestMapping("/detail/{aid}/{name}")
    public String detail(@PathVariable Integer aid,@PathVariable String name) {
        return name + aid;
    }


//    @RequestMapping("upload")
//    public String upload(@RequestPart("myfile")MultipartFile file) throws IOException {
//        String path = "c:\\Test\\img.png";
//        // 保存文件 上传文件一定使用post,get对文件大小有限制
//        file.transferTo(new File(path));
//        return path;
//    }

    @RequestMapping("upload")
    public String upload(@RequestPart("myfile")MultipartFile file) throws IOException {
        // 生成一个唯一的id  --- 时间戳/uid  uid 全球唯一id --- 保证在每时每刻生成的id都是唯一的
        String name = UUID.randomUUID().toString().replace("-","");
        // 得到源文件的后缀名
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filePath = name + type;
        // 唯一id + 后缀名
        String path = "c:\\Test\\" + filePath;
        // 保存文件 上传文件一定使用post,get对文件大小有限制
        file.transferTo(new File(path));
        return path;
    }

    @RequestMapping("/getcookie")
    public String getCookie(@CookieValue(value = "java",required = false)
                                        String bite) {
        return bite;
    }

    /**
     * 获取session
     * @param naem
     * @return
     */
    @RequestMapping("/getsession")
    public String getSession(@SessionAttribute(value ="SESSION_KEY",required = false)
                             String naem) {
        return naem;
    }


    /**
     * 设置session
     * @param request
     * @return
     */
    @RequestMapping("setsession")
    public String setSession(HttpServletRequest request) {

        // 此处不能得到请求,因为框架的前置方法执行在请求发送前
        HttpSession session = request.getSession();
        if (session != null) {
            session.setAttribute(_SESSION_KEY,"zhangsan");
            return "session set success";
        } else {
            return "session set fail";
        }

    }

    @ResponseBody
    @RequestMapping("getheader")
    public String getheader(@RequestHeader(value = "User-Agent",required = false) String header) {
        return header;
    }

}
